package com.kvest.gpsservicetracker.utils;

import android.os.Environment;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class LoggerService {
    private static final String TAG = "KVEST_TAG";

    private static LoggerService instance;
    private static final Object lock = new Object();

    private static String logFilePath;
    static {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            logFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Kvest_logs" + File.separator + "log.txt";
        } else {
            logFilePath = "";
        }
    }
    private static boolean writeToAndroidLog = true;
    private static boolean writeToFile = true;

    public List<LoggerListener> listeners;

    public static String getLogFilePath() {
        return logFilePath;
    }

    public static void setLogFilePath(String logFilePath) {
        LoggerService.logFilePath = logFilePath;
    }

    public static boolean isWriteToAndroidLog() {
        return writeToAndroidLog;
    }

    public static void setWriteToAndroidLog(boolean writeToAndroidLog) {
        LoggerService.writeToAndroidLog = writeToAndroidLog;
    }

    private DataOutputStream dos;
    private SimpleDateFormat sdf;

    public static LoggerService getInstance() {
        synchronized (lock) {
            if (instance == null) {
                instance = new LoggerService();
            }
        }

        return instance;
    }

    private LoggerService() {
        sdf = new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss] ");
        String fileName = getLogFilePath();
        if (fileName.equals("")) {
            return;
        }

        listeners = new LinkedList<LoggerListener>();

        File file = new File(fileName);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }

        try {
            dos = new DataOutputStream(new FileOutputStream(file, true));
        } catch (IOException ioException) {
            dos = null;
        }
    }

    public void writeLog(String value) {
        if (writeToAndroidLog) {
            Log.d(TAG, value);
        }

        if (writeToFile && dos != null) {
            try {
                dos.writeUTF(sdf.format(new Date()) + value + "\n");
            } catch (IOException ioException) {}
        }

        synchronized (listeners) {
            for (LoggerListener listener : listeners) {
                listener.onLogUpdated(value);
            }
        }
    }

    public void addLoggerListener(LoggerListener listener) {
        synchronized (listeners) {
            listeners.add(listener);
        }
    }

    public void removeLoggerListener(LoggerListener listener) {
        synchronized (listeners) {
            listeners.remove(listener);
        }
    }

    public interface LoggerListener {
        public void onLogUpdated(String value);
    }
}
